package com.sinoglobal.dumping.reciver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.sinoglobal.dumping.base.Dumpling_SharedPreferenceUtil;
import com.sinoglobal.dumping.base.Dumpling_SinoConstans;
import com.sinoglobal.dumping.base.Dumpling_SinoValueManager;
import com.sinoglobal.dumping.bean.Dumpling_UserInfoVo;
import com.sinoglobal.dumping.util.Dumpling_HttpMethodSet;
import com.sinoglobal.wallet.app.SinoValueManager;

/**
 * Created by KeZhuang on 2015/11/10.
 */
public class Dumpling_GetMoneyReceiver extends BroadcastReceiver {
    public static final String ACTION = Dumpling_SinoConstans.DUMPLING_GETMONEY_ACTION;
    public static OnLogInReceiverObserver or;
    public Dumpling_GetMoneyReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ACTION)) {
            saveUserInfo(intent, context);
           
        }
    }

    private void saveUserInfo(Intent intent, Context context) {
        Dumpling_UserInfoVo userInfoVo = (Dumpling_UserInfoVo) intent.getSerializableExtra("userInfo");
        if(userInfoVo!=null){
            Dumpling_SharedPreferenceUtil.saveUserName(context, userInfoVo.getUserName());
            Dumpling_SharedPreferenceUtil.saveUserId(context, userInfoVo.getId());
            Dumpling_SinoConstans.USER_ID = userInfoVo.getId();
            Dumpling_SinoConstans.USER_NAME = userInfoVo.getUserName();
            //init wallet  id 产品标识  电话
            SinoValueManager.getInstance().setIdProductPhone(context, Dumpling_SinoConstans.USER_ID, Dumpling_SinoConstans.PRODUCT_CODE, Dumpling_SinoConstans.USER_NAME);

            if (or!=null){
                if(!TextUtils.isEmpty(intent.getStringExtra("code"))){
                    or.onLoginObserver(userInfoVo);
                }
            }
            Dumpling_SinoValueManager.putValue(context, Dumpling_SinoConstans.DUMPLING_MARK_LOGIN, Dumpling_SinoConstans.LOGIN_STATE_LOGIN, true);
            Dumpling_HttpMethodSet.logoutUserGetMoney(context);
        }
 
    }
    
    public static void setOnLoginReceiverObserver(OnLogInReceiverObserver onLoginReceiverObserver){
        or = onLoginReceiverObserver;
    }
    public interface OnLogInReceiverObserver{
        void onLoginObserver(Dumpling_UserInfoVo vo);
    }

}
